﻿using System;

namespace Iot.Shared.Helper;

public static class IotTimestampHelper
{
    /// <summary>
    ///     获取当前时间
    /// </summary>
    /// <returns></returns>
    public static DateTime GetDateTime()
    {
        return DateTime.Now;
    }

    /// <summary>
    ///     采用Unix时间 的时间戳 秒
    /// </summary>
    /// <returns></returns>
    public static long GetUnixTimeSeconds()
    {
        return GetDateTime().GetUnixTimeSeconds();
    }

    /// <summary>
    ///     采用Unix时间的 时间戳毫秒
    /// </summary>
    /// <returns></returns>
    public static long GetUnixTimeMilliseconds()
    {
        return GetDateTime().GetUnixTimeMilliseconds();
    }

    /// <summary>
    ///     采用Unix时间的 时间戳 秒
    /// </summary>
    /// <returns></returns>
    public static long GetUnixTimeSeconds(this DateTime date)
    {
        return new DateTimeOffset(date).ToUnixTimeSeconds();
    }

    /// <summary>
    ///     采用Unix时间的 时间戳毫秒
    /// </summary>
    /// <returns></returns>
    public static long GetUnixTimeMilliseconds(this DateTime date)
    {
        return GetUnixTimeMilliseconds(new DateTimeOffset(date));
    }

    /// <summary>
    ///     采用Unix时间的 时间戳毫秒
    /// </summary>
    /// <returns></returns>
    public static long GetUnixTimeMilliseconds(this DateTimeOffset date)
    {
        return date.ToUniversalTime().ToUnixTimeMilliseconds();
    }

    /// <summary>
    ///     时间戳 秒
    /// </summary>
    /// <param name="seconds">秒</param>
    /// <returns></returns>
    public static DateTime UnixTimeSeconds(this long seconds)
    {
        return DateTimeOffset.FromUnixTimeSeconds(seconds).ToLocalTime().DateTime;
    }

    /// <summary>
    ///     时间戳毫秒
    /// </summary>
    /// <param name="milliseconds">毫秒</param>
    /// <returns></returns>
    public static DateTime UnixTimeMilliseconds(this long milliseconds)
    {
        return milliseconds.ConvertUnixMillisToLocal‌().DateTime;
    }

    /// <summary>
    ///     时间戳毫秒
    /// </summary>
    /// <param name="milliseconds">毫秒</param>
    /// <returns></returns>
    public static DateTimeOffset ConvertUnixMillisToLocal‌(this long milliseconds)
    {
        return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds).ToLocalTime();
    }
}